package com.x.Servlet_05_HttpServletApi;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * ClassName: HttpServletResponseApi
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.08.09 19:57
 */

@WebServlet("/servlet3")
public class HttpServletResponseApi extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String info = "<h2>hello</h2>";

        //设置响应行相关API
        //HTTP/1.1 200/404/405/500...
        //状态码：resp.setStatus(405);

        //设置响应头
        resp.setHeader("Date","2024-04-05");

            //代替：resp.setHeader("Content-Type", "text/html");
        resp.setContentType("text/html");
            //代替：resp.setHeader("Content-Length", "1234");
        resp.setContentLength(info.getBytes().length);//字符串转换成字节数组的长度


        //设置响应体内容API
        //获得一个向响应体输入文本的字符输出流
        PrintWriter writer = resp.getWriter();
        writer.println(info);

        //获得一个向响应体输入二进制信息的字节输出流
        //ServletOutputStream sos = resp.getOutputStream();





    }
}
